package com.jia.lidao.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.widget.Toast;

import com.jia.lidao.Constans;
import com.jia.lidao.R;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;

/**
 * Created by E0100291 on 2016/10/22.
 */

public class WXEntryUtil {

    public static IWXAPI api;

    public static void ShareCaseWebPageToWeixin(Context paramContext, Bitmap paramBitmap, String title, String description, String url)
    {
        baseShareWebPageToWeixin(paramContext, paramBitmap, title, description, url, false);
    }

    public static void ShareCaseWebPageToWeixinFriendGroup(Context paramContext, Bitmap paramBitmap, String title, String description, String url)
    {
        baseShareWebPageToWeixin(paramContext, paramBitmap, title, description, url, true);
    }

    private static void baseShareWebPageToWeixin(Context paramContext, Bitmap paramBitmap, String title, String description, String url, boolean isFriendGroup)
    {
        if (!isInstallWeixin(paramContext)) {
            Toast.makeText(paramContext, "未安装此应用~", Toast.LENGTH_SHORT).show();
            return;
        }
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(paramContext, Constans.APP_ID, false);
        WXWebpageObject localWXWebPageObject = new WXWebpageObject();

        localWXWebPageObject.webpageUrl = url;
        WXMediaMessage localWXMediaMessage = new WXMediaMessage(localWXWebPageObject);
        if (TextUtils.isEmpty(title)) {
            title ="粒到农资经销";
        }
        localWXMediaMessage.title = title;
        if (TextUtils.isEmpty(description)) {
            description ="粒到农资经销";
        }
        localWXMediaMessage.description = description;
        if (paramBitmap != null) {
            localWXMediaMessage.thumbData = WXEntryUtil.bmpToByteArray(paramBitmap, false);
        } else {
            Bitmap thumb = BitmapFactory.decodeResource(paramContext.getResources(), R.mipmap.wxlogo);
            localWXMediaMessage.thumbData = WXEntryUtil.bmpToByteArray(thumb, true);
        }
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = WXEntryUtil.buildTransaction("webpage");
        req.message = localWXMediaMessage;
        if (isFriendGroup) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        } else {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        }
        api.sendReq(req);
    }

    public static boolean isInstallWeixin(Context paramContext)    {
        PackageInfo localPackageInfo1 = null;
        try {
            localPackageInfo1 = paramContext.getPackageManager().getPackageInfo("com.tencent.mm", 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return localPackageInfo1 != null;
    }

    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}

